

import java.util.TreeSet;

public class Node implements Comparable<Node> {
	TreeSet<Node> childs = new TreeSet<Node>();
	int level = 0;
	Tile value = null;
	Node root = null;

	public Node(){
		
	}
	public Node(Tile value){
		setValue(value);
	}

	public void add(Node child) {
		child.level = this.level + 1;
		child.root = this;
		childs.add(child);
	}



	public Node find(Object value) {
		for (Node node : childs)
			if (node.getValue() != null)
				if (node.getValue().equals(value))
					return node;
				else {
					Node result = node.find(value);
					if (result != null)
						return result;
				}
		return null;
	}
	/**
	 * Localiza o valor buscado dentro dos nos , e preenche a rota percorrida para econtrar o no</br>
	 * OBS: so sera incluso no objeto route , os objetos Node que contiverem o resultado , ou seja : </br>
	 * todos no percurso , exceto o toot e o que contem o valor encontrado por serem respectivamente </br>
	 * start e end.
	 * 
	 * @param value
	 * @param route
	 * @return retorna um objeto {@link Node} com o valor buscado
	 */
	public Node find(Object value,TreeSet<Node> route) {
		for (Node node : childs)
			if (node.getValue() != null)
				if (node.getValue().equals(value)){
					//cancelado por ser sempre o valor de "start" route.add(this);
					//cancelado por ser sempre o valor de "end" route.add(node);
					return node;
				}
				else {
					Node result = node.find(value, route);
					if (result != null){
						route.add(this);
						return result;
					}
				}
		return null;
	}
	
	public static void main(String[] args) {
		
	}
	
/*################################################################################################*/
	/**
	 * Retorna a quandidade de nos neste no
	 * 
	 * @return
	 */
	public int getChildsCount() {
		return childs.size();
	}
	
	@Override
	public String toString(){
		return "Level : " + level + " , " + value.toString();
	}
	
	public void print(){
		String tab = "";
		for (int i = 0; i < this.level; i++) 
			tab += " ";
		for (Node node : childs) {
			System.out.println(tab + node);
		}
	}
	
	@Override
	public int compareTo(Node node) {
		return level > node.getLevel() ? 1 : (level == node
				.getLevel() ? 0 : -1);
	}

	public int getLevel() {
		return level;
	}
	public Tile getValue() {
		return value;
	}

	public void setValue(Tile value) {
		this.value = value;
	}

	public TreeSet<Node> getChilds() {
		return childs;
	}
}
